package com.syx.system.Manager;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.syx.common.redis.Service.RedisService;
import com.syx.commoncore.Constant.CacheConstant;
import com.syx.system.Mapper.user.UserMapper;
import com.syx.system.Model.user.User;
import com.syx.system.Model.user.VO.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class UserCacheManager {

    @Autowired
    private RedisService redisService;

    @Autowired
    private UserMapper userMapper;

    public void refreshUser(User user) {
        //刷新用户缓存

        String userKey = getUserKey(user.getUserId());
        User cacheObject = redisService.getCacheObject(userKey, User.class);
        if (cacheObject==null){
            return;
        }
        redisService.setCacheObject(userKey, user);
        //设置用户缓存有效期为10分钟
        redisService.expire(userKey, CacheConstant.USER_EXP, TimeUnit.MINUTES);
    }

    //u:d:用户id
    private String getUserKey(Long userId) {
        return CacheConstant.USER_DETAIL + userId;
    }
}
